import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import storage from './storage'
import axios from 'axios'
import VueAxios from 'vue-axios'
import env from './env'
import EU from 'element-ui'
import miheader from '@/components/miheader.vue'
import footermi from '@/components/footermi.vue'
import 'element-ui/lib/theme-chalk/index.css'
import header_logo from '@/components/header-logo.vue'
import topbar from '@/components/topbar.vue'
import searchBar from '@/components/searchBar.vue'
import '../resource/scss/config.scss'
import VueLazyload from 'vue-lazyload'

Vue.use(VueLazyload, { loading: 'img/loading-svg/loading-bars.svg' })

Vue.component('miheader', miheader)
Vue.prototype.$storage = storage
Vue.component('topbar', topbar)
Vue.component('footermi', footermi)
Vue.component('header-logo', header_logo)
Vue.component('search-bar', searchBar)
Vue.use(VueAxios, axios)
Vue.use(EU)

Vue.config.productionTip = false

axios.defaults.baseURL = '/api'
axios.defaults.timeout = 5000
axios.defaults.baseURL = env.baseURL
// console.log(axios.defaults.baseURL)
axios.interceptors.request.use(
  (req) => {
    // 没有return req会报错
    // console.log(req)
    return req
  },
  (err) => {
    // console.log(err)
    return Promise.reject(err)
  }
)
axios.interceptors.response.use(
  (data) => {
    // 在发送请求前要做的事儿
    // ...
    // console.log(req)
    // console.log(data)
    let path = location.hash
    let res = data.data
    if (res.status == 0) return res.data
    else if (res.status == 10) {
      if (path != '#/') {
        window.location.href = '/#/login'
      }
      return Promise.reject(res)
    } else {
      Message.warning(res.msg)
      return Promise.reject(res)
    }
  },
  (err) => {
    // 在请求错误时要做的事儿
    // ...
    // 该返回的数据则是axios.catch(err)中接收的数据
    // console.log(err)
    return Promise.reject(err)
  }
)

new Vue({
  router,
  store,
  storage,
  // state,
  render: (h) => h(App),
}).$mount('#app')
